package com.house365.im.client.listener;

import java.util.Random;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.content.SharedPreferences;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

import com.house365.core.R;
import com.house365.im.client.Constants;
import com.house365.im.client.XmppManager;
import com.house365.im.client.dto.NotificationData;
import com.house365.im.client.util.LogUtil;
import com.house365.im.client.util.SmileyParser;

/**
 * This class is to notify the user of messages with NotificationManager.
 * 
 * @modifier Rick Ping
 */
public class Notifier {

	private static final String LOGTAG = LogUtil.makeLogTag(Notifier.class);

	private static final Random random = new Random(System.currentTimeMillis());

	private XmppManager xmppManager;

	private SharedPreferences sharedPrefs;

	private NotificationManager notificationManager;

	private Context context;
	public Notifier(XmppManager xmppManager) {
		this.xmppManager = xmppManager;
		this.sharedPrefs = xmppManager.getImManager().getSharedPrefs();
		this.context = xmppManager.getContext();
		this.notificationManager = (NotificationManager) xmppManager.getContext()
				.getSystemService(Context.NOTIFICATION_SERVICE);
	}

	public void notify(NotificationData notificationData) {
		Log.d(LOGTAG, "notify()...");

		if (xmppManager.getImManager().isNotificationEnabled()) {

			// Notification
			Notification notification = new Notification();
			notification.icon = getNotificationIcon();
			notification.defaults = Notification.DEFAULT_LIGHTS;
			if (xmppManager.getImManager().isNotificationSoundEnabled()) {
//				notification.defaults |= Notification.DEFAULT_SOUND;
				
				notification.sound=Uri.parse("android.resource://" + context.getPackageName() + "/" +R.raw.msg2);
			}
			if (xmppManager.getImManager().isNotificationVibrateEnabled()) {
				notification.defaults |= Notification.DEFAULT_VIBRATE;
			}
			notification.flags |= Notification.FLAG_AUTO_CANCEL;
			notification.when = System.currentTimeMillis();
			notification.tickerText =  notificationData.getMessage();
			String callbackActivityClassName = sharedPrefs.getString(
					Constants.CALLBACK_ACTIVITY_CLASS_NAME, "");
			Class clazz=null;
			try {
				clazz = Class.forName(callbackActivityClassName);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			Intent intent = new Intent(context, clazz);
			intent.putExtra(Constants.NOTIFICATION_DATA,notificationData);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
			intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
			intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

			PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
					intent, PendingIntent.FLAG_UPDATE_CURRENT);

			notification.setLatestEventInfo(context, notificationData.getTitle(),notificationData.getMessage(),
					contentIntent);
			notificationManager.notify(random.nextInt(), notification);

		} 
	
	}

	private int getNotificationIcon() {
		return sharedPrefs.getInt(Constants.NOTIFICATION_ICON, 0);
	}


}
